package com.canyou.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.canyou.constants.CommonConstants;
import com.canyou.datascope.DataScope;
import com.canyou.system.entity.org.OrgDetailVO;
import com.canyou.system.entity.org.OrgListVO;
import com.canyou.system.mapper.DeptMapper;
import com.canyou.system.mapper.OrganizationMapper;
import com.canyou.system.model.Dept;
import com.canyou.system.model.Organization;
import com.canyou.system.service.OrganizationService;
import com.canyou.system.service.UserService;
import com.canyou.utils.AbstractTreeNode;
import com.canyou.utils.TreeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 机构服务实现类
 *
 * @author canyou
 * @since 2019-06-03
 */
@Service
public class OrganizationServiceImpl extends ServiceImpl<OrganizationMapper, Organization> implements OrganizationService {

    @Autowired
    private UserService userService;

    @Resource
    private DeptMapper deptMapper;

    @Override
    public List<OrgListVO> getOrgTree(String orgName, String contacts) {
        List<OrgListVO> list = baseMapper.getOrgList(new DataScope().setScopeName("id"), orgName, contacts);
        List<OrgListVO> tree = TreeUtils.build(list, getOrgParentId(userService.getCurrentUserOrgId()));
        return tree;
    }

    @Override
    public boolean existOrg(String orgName, Long orgId) {
        LambdaQueryWrapper<Organization> queryWrapper = Wrappers.<Organization>lambdaQuery()
                .eq(Organization::getOrgName, orgName);
        if (orgId != null) {
            queryWrapper.ne(Organization::getId, orgId);
        }
        return baseMapper.selectCount(queryWrapper) > 0;
    }

    @Override
    public OrgDetailVO getDetailById(Long orgId) {
        return baseMapper.getOrgDetail(orgId);
    }

    @Override
    public long getChildCount(Long parentId) {
        LambdaQueryWrapper<Organization> queryWrapper = Wrappers.<Organization>lambdaQuery()
                .eq(Organization::getParentId, parentId);
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    public Long getOrgParentId(Long orgId) {
        if (orgId == null) {
            throw new RuntimeException("orgId不能为空");
        }
        Organization org = baseMapper.selectById(orgId);
        if (org == null) {
            return null;
        }
        return org.getParentId();
    }

    @Override
    public long getOrgDeptCount(Long orgId) {
        LambdaQueryWrapper<Dept> queryWrapper = Wrappers.<Dept>lambdaQuery()
                .eq(Dept::getOrgId, orgId);
        return deptMapper.selectCount(queryWrapper);
    }

    @Override
    public long getOrgUserCount(Long orgId) {
        return userService.getUserCountByOrgId(orgId);
    }

    @Override
    public boolean removeById(Long orgId) {
        return baseMapper.deleteById(orgId) > 0;
    }

    @Override
    public List<OrgListVO> getOrgAndChildList(Long rootId) {
        List<OrgListVO> list = baseMapper.getAllOrgListVO();
        List<OrgListVO> newList = new ArrayList<>();
        // 将自己本身加入到集合中
        if (!rootId.equals(CommonConstants.DEFAULT_PARENT_ID)) {
            newList.addAll(list.stream()
                    .filter(org -> rootId.equals(org.getId()))
                    .collect(Collectors.toList()));
        }
        TreeUtils.buildList(list, newList, rootId);
        return newList;
    }

    @Override
    public List<Long> getOrgAndChildIdList(Long rootId) {
        return getOrgAndChildList(rootId).stream()
                .map(AbstractTreeNode::getId)
                .collect(Collectors.toList());
    }
}
